<template>
  <el-dialog
    :model-value="visible"
    title="权限修改"
    width="600"
    :close-on-click-modal="false"
    @close="cls"
  >
    <el-form
      ref="formRel"
      prop="name"
      :model="data"
      label-width="100px"
      style="max-width: 600px"
    >
      <el-form-item label="型号:">
        <el-select v-model="data.roleId" placeholder="请选择">
          <el-option
            v-for="(item, index) in items"
            :key="index"
            :label="item"
            :value="index + 1"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button type="default" @click="cls">取消</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from "vue";
import { updateRoleId } from "@/api/users.js";
// 在你的组件代码中添加
import { ElMessage, roleTypes } from "element-plus";

//表单引用
const formRel = ref(null);
const items = ["管理员", "客户", "供应商", "货代", "销售"];

const data = ref({
  roleId: "",
  id: ""
});
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
});
const emits = defineEmits(["close", "refresh"]);

function cls() {
  emits("close");
}
//保存
async function save() {
  //验证规则 为false时，不会触发表单提交
  if (data.value.roleId == null) return;
  let res = await updateRoleId(data.value);
  console.log(data.value);
  console.log(res);

  if (res.code === 200) {
    ElMessage({
      message: "修改成功",
      type: "success"
    });
    //刷新部门列表
    emits("refresh");
    cls();
  }
}

function init(id, roleId) {
  //编辑部门
  data.value.roleId = roleId;
  data.value.id = id;
}
defineExpose({ init });
</script>

<style scoped></style>
